package vista;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import util.Utilidades;

import modelo.Cliente;
import modelo.Solicitud;
import modelo.Status;
import modelo.dao.Servidor;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class DlgSeleccionarSolicitud extends javax.swing.JDialog {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JButton btSalir;
	private JButton btAceptar;
	private JScrollPane jScrollPane1;
	private JButton btBuscar;
	private JButton btVer;
	private JLabel lblCodigo;
	private JTextField txtCodigo;
	private JTable tbSolicitudes;
	private ArrayList<Solicitud> solicitudes;
	private AbstractTableModel tbModelSolicitudes;
	private Solicitud solicitud=null;

	/**
	* Auto-generated main method to display this JDialog
	*/
	/*public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JFrame frame = new JFrame();
				DlgSeleccionarSolicitud inst = new DlgSeleccionarSolicitud(frame);
				inst.setVisible(true);
			}
		});
	}*/
	
	public DlgSeleccionarSolicitud(JFrame frame) {
		super(frame);
		initGUI();
	}
	
	private void initGUI() {
		try {
			{
				getContentPane().setLayout(null);
				this.setTitle("Seleccionar Solicitud");
				{
					btSalir = new JButton();
					getContentPane().add(btSalir);
					btSalir.setText("Salir");
					btSalir.setIcon(new ImageIcon(getClass().getClassLoader().getResource("iconos/Salir.gif")));
					btSalir.setHorizontalTextPosition(JButton.CENTER);
					btSalir.setVerticalTextPosition(JButton.BOTTOM);
					btSalir.setBounds(375, 244, 79, 40);
				}
				{
					btAceptar = new JButton();
					getContentPane().add(btAceptar);
					btAceptar.setText("Aceptar");
					btAceptar.setIcon(new ImageIcon(getClass().getClassLoader().getResource("iconos/Aceptar.png")));
					btAceptar.setHorizontalTextPosition(JButton.CENTER);
					btAceptar.setVerticalTextPosition(JButton.BOTTOM);
					btAceptar.setBounds(269, 244, 79, 40);
				}
				{
					jScrollPane1 = new JScrollPane();
					getContentPane().add(jScrollPane1);
					jScrollPane1.setBounds(34, 79, 410, 121);
					{
						
						solicitudes = Servidor.solicitudes().consultarSolicitudesPorDetallesConStatus(Status.ACTIVO);

						tbModelSolicitudes = new AbstractTableModel() {
							@Override
							public Object getValueAt(int fil, int col) {
								switch (col) {
								case 0: return String.format("%04d", solicitudes.get(fil).getCodigo());
								case 1: return solicitudes.get(fil).getDescripcion();
								}
								
								return null;
							}
							
							@Override
							public int getRowCount() {
								return solicitudes.size();
							}
							
							@Override
							public String getColumnName(int col) {
								switch (col) {
								case 0: return "Codigo";
								case 1: return "Especificacion";
								}
								
								return null;
							}
							@Override
							public int getColumnCount() {
								return 2;
							}
						};
						tbSolicitudes = new JTable();
						jScrollPane1.setViewportView(tbSolicitudes);
						tbSolicitudes.setModel(tbModelSolicitudes);
						tbSolicitudes.setBounds(66, 83, 352, 170);
					}
				}
				{
					btBuscar = new JButton();
					getContentPane().add(btBuscar);
					btBuscar.setIcon(new ImageIcon(getClass().getClassLoader().getResource("iconos/buscar.PNG")));
					btBuscar.setBounds(281, 26, 41, 27);
				}
				{
					txtCodigo = new JTextField();
					getContentPane().add(txtCodigo);
					txtCodigo.setBounds(181, 26, 88, 28);
				}
				{
					lblCodigo = new JLabel();
					getContentPane().add(lblCodigo);
					lblCodigo.setText("Numero:");
					lblCodigo.setFont(new java.awt.Font("Tahoma",1,11));
					lblCodigo.setBounds(103, 33, 66, 14);
				}
				{
					btVer = new JButton();
					getContentPane().add(btVer);
					btVer.setText("Ver");
					btVer.setIcon(new ImageIcon(getClass().getClassLoader().getResource("iconos/Ver.png")));
					btVer.setVerticalTextPosition(JButton.BOTTOM);
					btVer.setHorizontalTextPosition(JButton.CENTER);
					btVer.setBounds(160, 244, 79, 40);
					
				}
			}
			this.setSize(474, 354);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	DlgSeleccionarSolicitud dssol=this;
	public void mostrar(JFrame frame) {
		
		setLocationRelativeTo(null);
		darFormatos();
		
		btBuscar.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(validarFormato())
					buscar();
			}
		});
		
		txtCodigo.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent ke) {
				if (ke.getKeyChar()== KeyEvent.VK_ENTER) {
					if(validarFormato())
					   buscar();
				}
			}
		});
		
		btVer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				DlgSolicitud dsol= new DlgSolicitud();
				if(Utilidades.seleccion(tbSolicitudes, dssol, "Solicitud"))
				{ Solicitud sol=solicitudes.get(dssol.tbSolicitudes.getSelectedRow());
				  dsol.mostrarVer(dssol, sol);
				}
			}
		});
		
		btAceptar.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(Utilidades.seleccion(tbSolicitudes, dssol, "Solicitud"))
				{ 	 solicitud=solicitudes.get(dssol.tbSolicitudes.getSelectedRow());
				      DlgSeleccionarDetalleSolicitud dsdsol=new DlgSeleccionarDetalleSolicitud(dssol,solicitud);
				      dsdsol.mostrar(dssol);
				      solicitudes = Servidor.solicitudes().consultarSolicitudesPorDetallesConStatus(Status.ACTIVO);
				      tbModelSolicitudes.fireTableDataChanged();
				}
			}
		});
		
		btSalir.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				dssol.dispose();
			}
		});
		

		this.setModalityType(ModalityType.APPLICATION_MODAL);
		setVisible(true);
		
	}
	
	public void buscar()
	{  if(!(Utilidades.campoVacio(dssol, txtCodigo, "Numero")))
		 {	
			boolean b=false;
		    int i=0;
		    while(i<solicitudes.size() && !b)
		    {	if(solicitudes.get(i).getCodigo()==Integer.parseInt(txtCodigo.getText()))
		    		b=true;
		    	++i;
		    }
		    if(b)
		    	tbSolicitudes.setRowSelectionInterval(i-1, i-1);
			else
				JOptionPane.showMessageDialog(dssol, "El numero de solicitud no se encuentra","Registro no encontrado",JOptionPane.INFORMATION_MESSAGE);
		}
	}
	
	public boolean validarFormato(){ 
		String c=txtCodigo.getText();
		if(c.length()!=4){	
			JOptionPane.showMessageDialog(dssol, "El numero de solicitud debe ser de 4 digitos","Formato no permitido",JOptionPane.INFORMATION_MESSAGE);
		    return false;
		}	
		else{
			if(c.equalsIgnoreCase("0000")){
				JOptionPane.showMessageDialog(dssol, "El ultimo numero debe ser mayor que cero","Formato no permitido",JOptionPane.INFORMATION_MESSAGE);
		    	return false;
			}
		    else
		    	return true;
		}
			
		
		
	}
	public void darFormatos(){ 
		Utilidades.soloNumeros(txtCodigo);
		Utilidades.limitarCaracteres(txtCodigo,4);
		
	 }
	

}
